package com.hsp.smallChange.oop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

//面向对象编程
public class SmallChangeSysOOP {
    //属性
    Scanner scanner = new Scanner(System.in);
    String key = "";
    boolean loop = true;
    //2. 完成零钱通明细：（1）可以把收益入账和消费，保存到数组（2）可以使用对象（3）可以使用String拼接
    String details = "----------------零钱通明细-----------------";
    //3. 收益入账
    double money = 0;
    double balance = 0;
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//可以用于日期格式的转换
    //4. 消费
    //定义新变量，保存消费的原因
    String note = null;

    public void mainMenue() {
        System.out.println("显示零钱通菜单...");
        do {
            System.out.println("==========选择零钱通菜单(OOP)==========");
            System.out.println("\t\t1. 零钱通明细");
            System.out.println("\t\t2. 收益入账");
            System.out.println("\t\t3. 消费");
            System.out.println("\t\t4. 退出");

            System.out.print("请选择1-4");
            key = scanner.next();
            //使用switch分支结构
            switch (key) {
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;

                case "3":
                    this.pay();
                    break;

                case "4":
                    this.exit();
                    //当用户退出循环以后，在判断
                    break;
                default:
                    System.out.println("菜单选择有误，请重新选择");
            }
        } while (loop);
    }



    //完成零钱通明细
    public void detail() {
        System.out.println(details);
    }

    //完成收益入账
    public void income() {
        System.out.println("收益入账金额");
        money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("收益入账金额需要大于零");
            return;//退出方法，不再执行后面的代码
        }
        //money的值校验

        date = new Date();
        balance += money;
        //拼接收益入账信息到details
        details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;
    }
    //消费
    public void pay() {
        System.out.print("消费金额");
        money = scanner.nextDouble();
        //范围校验,找出金额不正确的情况
        if (money <= 0 || money > balance) {
            System.out.println("消费金额应该在0-" + balance);
            return;
        }

        System.out.println("消费说明：");
        note = scanner.next();
        balance -= money;
        //拼接消费信息
        date = new Date();
        details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    //退出
    public void exit() {
        String choice;
        //判断用户是否要退出
        while (true) {
            System.out.print("你确定要退出吗？ y/n\t");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }
        if(choice.equals("y")){
            loop=false;
            return;
        }
    }
}
